import binary.decodeVarUint
import kotlin.test.Test
import kotlin.test.asserter


/**
 *@author haypo
 *@since 2021/7/20
 */
class LEB128Test {
    @Test
    fun TestDecodeVarUint() {
        val data = arrayOf(
            (0b1_0111111).toByte(),
            0b1_0011111.toByte(),
            0b1_0001111.toByte(),
            0b1_0000111.toByte(),
            0b1_0000011.toByte(),
            0b0_0000001.toByte()
        ).toByteArray()

        TestDecodeVarUint32(data.drop(5).toByteArray(), 0b0000001.toUInt(), 1)
        TestDecodeVarUint32(data.drop(4).toByteArray(), 0b1_0000011.toUInt(), 2)
        TestDecodeVarUint32(data.drop(3).toByteArray(), 0b1_0000011_0000111.toUInt(), 3)
        TestDecodeVarUint32(data.drop(2).toByteArray(), 0b1_0000011_0000111_0001111.toUInt(), 4)
        TestDecodeVarUint32(data.drop(1).toByteArray(), 0b1_0000011_0000111_0001111_0011111.toUInt(), 5)
    }

    private fun TestDecodeVarUint32(data: ByteArray, n: UInt, w: Int) {
        val (_n, _w) = decodeVarUint(data, 32)
        asserter.assertEquals("", n, _n.toUInt())
        asserter.assertEquals("", w, _w)
    }
}